<route lang="jsonc" type="page">
    {
      "layout": "default",
      "style": {
        "navigationBarTitleText": "支付",
        "navigationBarBackgroundColor": "#ffffff",
        "navigationBarTextStyle": "black"
      }
    }
</route>

<script lang="ts" setup>
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';
// 导入添加问诊记录的API（假设存在该API）
import { addConsultOrder } from '@/api/inquiry';

// 协议同意状态
const agreeProtocol = ref(false);
// 支付方式弹窗显示状态
const showPaymentMethod = ref(false);
// 选中的支付方式
const selectedPaymentMethod = ref('wechat');

// 支付金额数据
const paymentInfo = {
  consultationFee: 39,
  coupon: 0,
  pointsDeduction: 0,
  totalPayment: 39
};

// 患者信息 - 初始为空
const patientInfo = ref<any>({});
// 病情信息 - 初始为空
const consultationInfo = ref<any>({});

// 路由实例
const router = useRouter();

// 页面加载时从本地存储读取数据
onMounted(() => {
  try {
    // 读取患者信息
    const patientData = uni.getStorageSync('patientInfo');
    if (patientData) {
      patientInfo.value = patientData;
      console.log('从本地存储读取患者信息:', patientInfo.value);
    } else {
      console.warn('本地存储中未找到患者信息');
    }

    // 读取病情信息
    const consultationData = uni.getStorageSync('consultationInfo');
    if (consultationData) {
      consultationInfo.value = consultationData;
      console.log('从本地存储读取病情信息:', consultationInfo.value);
    } else {
      console.warn('本地存储中未找到病情信息');
    }
  } catch (e) {
    console.error('读取本地存储数据失败:', e);
    uni.showToast({
      title: '数据读取失败',
      icon: 'none'
    });
  }
});

const handleProtocolChange = (e) => {
  agreeProtocol.value = e.detail.value.length > 0;
  console.log('协议状态:', agreeProtocol.value);
};

// 立即支付按钮点击事件
const handlePayment = () => {
  if (!agreeProtocol.value) {
    uni.showToast({
      icon: 'none',
      title: '请先同意支付协议'
    });
    return;
  }
  // 显示支付方式选择框
  showPaymentMethod.value = true;
  // 添加弹窗显示的动画类
  setTimeout(() => {
    const modal = document.querySelector('.payment-method-modal');
    if (modal) {
      modal.classList.add('modal-visible');
    }
  }, 10);
};
// 关闭弹窗
const closeModal = () => {
  const modal = document.querySelector('.payment-method-modal');
  if (modal) {
    modal.classList.remove('modal-visible');
    setTimeout(() => {
      showPaymentMethod.value = false;
    }, 300);
  }
};

// 确认支付方式并支付
const confirmPayment = () => {
  closeModal();

  // 这里可以添加实际的支付逻辑
  console.log('发起支付请求', {
    amount: paymentInfo.totalPayment,
    patient: patientInfo,
    paymentMethod: selectedPaymentMethod.value
  });

  // 模拟支付成功后的跳转
  uni.showLoading({
    title: '支付中...'
  });

  setTimeout(() => {
    uni.hideLoading();
   
   // 创建要添加的问诊记录数据
    const orderData = {
      // patientId: patientInfo.value.id,
      name: patientInfo.value.name,
      idCard: patientInfo.value.idCard,
      defaultFlag: patientInfo.value.defaultFlag,
      // patientName: patientInfo.value.name,
      gender: patientInfo.value.gender,
      // age: patientInfo.value.age,
      // illnessDesc: consultationInfo.value.description,
      // amount: paymentInfo.totalPayment,
      // paymentMethod: selectedPaymentMethod.value,
      // createTime: new Date().toISOString(),
      // status: 4, // 假设4表示已完成
      // statusValue: '已完成',
      // type: '2', // 极速问诊类型
    };

    // 调用API添加问诊记录
    addConsultOrder(orderData)
      .then(res => {
        if (res.code === 10000) {
          console.log('问诊记录添加成功', res.data);
          // 跳转到支付成功页面
          router.push('/pages/paysuccess/paysuccess');
        }
      })
      .catch(error => {
        console.error('添加问诊记录失败', error);
        uni.showToast({
          title: '网络异常，请重试',
          icon: 'none'
        });
      });

    // 跳转到支付成功页面
    router.push('/pages/paysuccess/paysuccess');
  }, 1500);
};
</script>

<template>
  <view class="payment-container">
    <!-- 支付内容区域 -->
    <view class="payment-content">
      <!-- 问诊类型和价格 -->
      <view class="consultation-type">
        <text class="type-text">图文问诊 </text>
        <text class="price-text">{{ paymentInfo.consultationFee }} 元</text>
      </view>

      <!-- 极速问诊提示 -->
      <view class="quick-consultation">
        <img src="/static/image/doctor_portrait@1x.png" alt="医生图标" class="doctor-icon">
        <view>
            <view class="quick-text">极速问诊</view>
        <view class="auto-assign">自动分配医生</view>
        </view>
      </view>

      <!-- 优惠券和积分 -->
      <view class="discount-items">
        <view class="discount-item">
          <text class="item-name">优惠券</text>
          <text class="item-value">-¥{{ paymentInfo.coupon }}</text>
        </view>
        <view class="discount-item">
          <text class="item-name">积分抵扣</text>
          <text class="item-value">-¥{{ paymentInfo.pointsDeduction }}</text>
        </view>
      </view>

      <!-- 实付款 -->
      <view class="actual-payment">
        <text class="payment-label">实付款</text>
        <text class="payment-value">¥{{ paymentInfo.totalPayment }}</text>
      </view>

      <!-- 分割线 -->
      <view class="divider"></view>

      <!-- 患者信息 -->
      <view class="patient-info">
        <text class="section-title">患者信息</text>
        <text class="info-value">{{ patientInfo.name }} | {{ patientInfo.genderValue }} | {{ patientInfo.age }}</text>
      </view>

      <!-- 病情描述 -->
      <view class="illness-description">
        <text class="section-title">病情描述</text>
        <text class="description-value">{{ consultationInfo.description }}</text>
      </view>

      <!-- 支付协议 -->
      <view class="payment-protocol">
  <checkbox-group @change="handleProtocolChange">
    <label>
      <checkbox :checked="agreeProtocol" />
      <text class="protocol-text">我已同意</text>
      <text class="protocol-link">支付协议</text>
    </label>
  </checkbox-group>
</view>

      <!-- 合计和支付按钮 -->
      <view class="total-and-pay">
        <view class="total-amount">
          <text class="total-label">合计:</text>
          <text class="total-value">¥{{ paymentInfo.totalPayment }}.00</text>
        </view>
        <button class="pay-button" @click="handlePayment">立即支付</button>
      </view>
    </view>
    <!-- 支付方式选择弹窗 -->
    <view class="payment-method-modal" v-if="showPaymentMethod" @click="closeModal">
      <view class="modal-overlay"></view>
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <view class="modal-title">选择支付方式</view>
          <view class="close-btn" @click="closeModal">×</view>
        </view>
        <view class="amount-display">¥{{ paymentInfo.totalPayment }}.00</view>
        <view class="payment-methods">
          <view class="method-item" :class="{ 'selected': selectedPaymentMethod === 'wechat' }" @click="selectedPaymentMethod = 'wechat'">
            <view class="method-icon">
              <img src="/static/image/weixin.png" alt="微信支付" />            
            </view>
            <view class="method-name">微信支付</view>
            <view class="selected-icon" v-if="selectedPaymentMethod === 'wechat'"></view>
          </view>
          <view class="method-item" :class="{ 'selected': selectedPaymentMethod === 'alipay' }" @click="selectedPaymentMethod = 'alipay'">
            <view class="method-icon">
              <img src="/static/image/zhifubao.png" alt="支付宝支付" />            
            </view>
            <view class="method-name">支付宝支付</view>
            <view class="selected-icon" v-if="selectedPaymentMethod === 'alipay'"></view>
          </view>
        </view>
        <button class="confirm-pay-btn" @click="confirmPayment">立即支付</button>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.payment-container {
  position: relative;
  padding: 0 30rpx;
  background-color: #ffffff;
  min-height: 100vh;
}
.payment-protocol {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
  margin-left: 0; // 确保复选框在正确位置
}

.back-btn {
  position: absolute;
  top: 30rpx;
  left: 30rpx;
  z-index: 10;
}

.back-icon {
  width: 40rpx;
  height: 40rpx;
}

.payment-content {
  padding-top: 100rpx;
  padding-bottom: 180rpx;
}

.consultation-type {
  align-items: center;
  padding: 30rpx 0;
  margin-top: -100rpx;
  border-top: 1rpx solid #f0f0f0;
}

.type-text {
  font-size: 36rpx;
  color: #333333;
}

.price-text {
  font-size: 15px;
//   font-weight: bold;
  color: #333333;
}

.quick-consultation {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.doctor-icon {
  width: 80rpx;
  height: 80rpx;
  margin-right: 15rpx;
  border-radius: 50%;
}

.quick-text {
  font-size: 32rpx;
  color: #333333;
  margin-right: 10rpx;
}

.auto-assign {
  font-size: 28rpx;
  color: #999999;
}

.discount-items {
  padding: 30rpx 0;
//   border-bottom: 1rpx solid #f0f0f0;
}

.discount-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 45rpx;
  padding-top: 16rpx;
}

.discount-item:last-child {
  margin-bottom: 0;
}

.item-name {
  font-size: 32rpx;
  color: #666666;
}

.item-value {
  font-size: 32rpx;
  color: #666666;
}

.actual-payment {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 0;
}

.payment-label {
  font-size: 32rpx;
  color: #666666;
}

.payment-value {
  font-size: 35rpx;
  // font-weight: bold;
  color: #e61e1e;
}

.divider {
  height: 20rpx;
  background-color: #f5f5f5;
  margin: 15rpx 0;
}

.section-title {
  font-size: 32rpx;
  color: #999999;
  margin-bottom: 15rpx;
  display: block;
}

.patient-info {
  padding: 25rpx 0;
  display: flex;
  justify-content: space-between;
  border-bottom: 1rpx solid #f0f0f0;
}

.info-value {
  font-size: 32rpx;
  color: #333333;
}

.illness-description {
  padding: 30rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.description-value {
  font-size: 32rpx;
  color: #333333;
  line-height: 1.5;
}

.protocol-checkbox {
  transform: scale(0.8);
  margin-right: 10rpx;
  z-index: 1;
  position: relative;
}

.protocol-text {
  font-size: 28rpx;
  color: #666666;
}

.protocol-link {
  font-size: 28rpx;
  color: #11c1a2;
}

.total-and-pay {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 30rpx;
  background-color: #ffffff;
//   box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.total-amount {
  display: flex;
  align-items: center;
  margin-right: 140rpx;
}

.total-label {
  font-size: 32rpx;
  color: #666666;
  margin-right: 10rpx;
}

.total-value {
  font-size: 40rpx;
  font-weight: bold;
  color: #e61e1e;
}

.pay-button {
  width: 340rpx;
  height: 80rpx;
  background-color: #11c1a2;
  color: #ffffff;
  font-size: 32rpx;
  border-radius: 45rpx;
  line-height: 80rpx;
  text-align: center;
  border: none;
}

// 支付方式弹窗样式
.payment-method-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.payment-method-modal.modal-visible {
  opacity: 1;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  transition: background-color 0.3s ease;
}

.modal-content {
  width: 100%;
  background-color: #ffffff;
  border-radius: 30rpx 30rpx 0 0;
  padding: 30rpx;
  transform: translateY(100%);
  transition: transform 0.3s ease;
  max-height: 80vh;
  overflow-y: auto;
}

.payment-method-modal.modal-visible .modal-content {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.modal-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
}

.close-btn {
  font-size: 48rpx;
  color: #999999;
  width: 60rpx;
  height: 60rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.amount-display {
  font-size: 48rpx;
  font-weight: bold;
  color: #e61e1e;
  text-align: center;
  margin: 40rpx 0;
}

.payment-methods {
  margin-bottom: 40rpx;
}

.method-item {
  display: flex;
  align-items: center;
  padding: 30rpx 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
  position: relative;
}

.method-item.selected {
  background-color: rgba(17, 193, 162, 0.05);
}

.method-icon {
  width: 60rpx;
  height: 60rpx;
  margin-right: 30rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.method-icon img {
  width: 100%;
  height: 100%;
}

.method-name {
  flex: 1;
  font-size: 32rpx;
  color: #333333;
}

.selected-icon {
  width: 36rpx;
  height: 36rpx;
  background-color: #11c1a2;
  border-radius: 50%;
  position: relative;
}

.selected-icon::after {
  content: '';
  position: absolute;
  top: 6rpx;
  left: 12rpx;
  width: 12rpx;
  height: 20rpx;
  border: solid white;
  border-width: 0 4rpx 4rpx 0;
  transform: rotate(45deg);
}

.confirm-pay-btn {
  width: 100%;
  height: 90rpx;
  background-color: #11c1a2;
  color: #ffffff;
  font-size: 32rpx;
  border-radius: 45rpx;
  border: none;
  margin-top: 20rpx;
}

.pay-button {
  width: 340rpx;
  height: 80rpx;
  background-color: #11c1a2;
  color: #ffffff;
  font-size: 32rpx;
  border-radius: 40rpx;
  border: none;
}
</style>